//
//  CPMineVC.m
//  CPApp
//
//  Created by 健健 on 2018/3/21.
//  Copyright © 2018年 Class. All rights reserved.
//

#import "CPMineVC.h"
#import "CPAboutController.h"
#import "CPEditInfoVC.h"
#import "CPEditPasswordVC.h"
#import "CPLoginVC.h"

#define MineSB [UIStoryboard storyboardWithName:@"Mine" bundle:nil]

@interface CPMineVC ()<UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *infoImage;
@property (weak, nonatomic) IBOutlet UILabel *username;
@property (strong, nonatomic) NSString *rightTitle;
@property (weak, nonatomic) IBOutlet UILabel *cache;

@end

@implementation CPMineVC
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    if ([self isLogin] == YES) {
        self.rightTitle = @"退出登录";
    }else {
        self.rightTitle = @"登录";
    }
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:self.rightTitle style:UIBarButtonItemStylePlain target:self action:@selector(rightBtn)];
    
    self.username.text = [mUserDefaults objectForKey:@"username"];
    
}
-(BOOL)isLogin {
    BOOL isLogin = [[mUserDefaults objectForKey:@"login"] boolValue];
    if (isLogin) {
        return YES;
    }
    return NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我";
    self.tableView.tableFooterView = [UIView new];
    self.cache.text = [NSString stringWithFormat:@"%.2fMB",[CommonTools readCacheSize]];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row==1) {
        if ([self isLogin] != YES) {
            [ProgressHUD show:@"请登录"];return;
        }
        CPEditInfoVC * infoVC = [MineSB instantiateViewControllerWithIdentifier:@"EditInfo"];
        [self.navigationController pushViewController:infoVC animated:YES];
    }else if (indexPath.row==2) {
        if ([self isLogin] != YES) {
            [ProgressHUD show:@"请登录"];return;
        }
        CPEditPasswordVC * passwordVC = [MineSB instantiateViewControllerWithIdentifier:@"EditPassword"];
        [self.navigationController pushViewController:passwordVC animated:YES];
    }else if (indexPath.row==3) {
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"是否清理缓存？" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
    }else if (indexPath.row==4) {
        CPAboutController * aboutVC =[MineSB instantiateViewControllerWithIdentifier:@"About"];
        [self.navigationController pushViewController:aboutVC animated:YES];
    }else {
        return;
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex==1) {
        [CommonTools clearFile];
        self.cache.text = @"0.00MB";
    }
}
- (IBAction)signbtn:(UIButton *)sender {
    if (![self isLogin] == YES) {
        [ProgressHUD show:@"请登录"];return;
    }
    sender.selected = !sender.selected;
    BOOL isSign = [[AVUser user] signUp:nil];
    if (isSign) {
        NSLog(@"签到成功");
    }else {
        NSLog(@"签到失败");
    }
    [ProgressHUD show:@"签到成功"];
}

-(void)rightBtn {
    if ([self isLogin] == YES) {
        [mUserDefaults setObject:@"0" forKey:@"login"];
        [AVUser logOut];
         self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"登录" style:UIBarButtonItemStylePlain target:self action:@selector(rightBtn)];
        self.username.text = @"昵称";
        [mUserDefaults setObject:@"昵称" forKey:@"username"];
        return;
    }
    CPLoginVC * loginVC =[MineSB instantiateViewControllerWithIdentifier:@"Login"];
    loginVC.title = @"登录";
    [self.navigationController pushViewController:loginVC animated:YES];
}

@end
